package cn.afactory.tygz.ui.sns;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.Timer;
import java.util.TimerTask;

import cn.afactory.tygz.R;
import cn.afactory.tygz.model.SnsCommentInfo;
import cn.afactory.tygz.net.NetManager;
import cn.afactory.tygz.net.lsn.OnResponseListener;
import cn.afactory.tygz.ui.base.BaseActivity;
import cn.afactory.tygz.ui.base.BaseFragmentActivity;
import cn.afactory.tygz.ui.base.utils.DialogUtil;
import cn.afactory.tygz.ui.base.utils.LoadingDialog;
import cn.afactory.tygz.ui.base.utils.LocalUtils;
import cn.afactory.tygz.ui.base.utils.SPUtils;
import cn.afactory.tygz.ui.base.views.RestrictEditTextView;
import cn.afactory.tygz.ui.sns.fragment.SubjectDetailFragment;

/**
 * Created by Kenneth on 15/9/22.
 */
public class SendCommentActivity extends Activity {
    public static final String SUBJECT_ID = "subject_id";
    public static final String COMMENT_DATA = "comment_data";
    public static final String DATA = "data";

    private RestrictEditTextView mText;
    private TextView mSend;
    private TextView mCancel;

    private long subjectId;
    private SnsCommentInfo mInfo = null ;

    protected LoadingDialog mLoadingDialog;
    protected Context mContext;

    private int type = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.square_detail_send_text);
        subjectId = getIntent().getLongExtra(SPUtils.EXTRA_POST_ID, 0);
        type = getIntent().getIntExtra(SPUtils.EXTRA_TYPE,0);
        mText = (RestrictEditTextView)findViewById(R.id.msg_text);
        mSend = (TextView)findViewById(R.id.msg_send);
        mSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mText.getText() != null && mText.getText().length() > 0) {
                    if(mText.getText().length() <= 100) {
                        doPublishComment();
//                        SendCommentActivity.this.setResult(RESULT_OK, intent);
                        setResult(SubjectDetailFragment.REQUEST_CODE_COMMENT);
                        finish();
                    } else {
                        LocalUtils.showToast(SendCommentActivity.this, getString(R.string.square_comment_over_length));
                    }
                }
            }
        });
        mCancel = (TextView)findViewById(R.id.msg_cancel);
        mCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!isFinishing()) {
                    SendCommentActivity.this.finish();
                }
            }
        });
    }

    private void doPublishComment(){
        String msgContent = mText.getText().toString();
        if(msgContent == null){
            LocalUtils.showToast(this,"亲，捎带说几句!");
            return;
        }
        showLoadingView("发布中...,请稍候！");
        if(type == 0) {
            NetManager.getInstance(this).doPostComment(subjectId, msgContent, new OnResponseListener<Boolean>() {
                @Override
                public void onComplete(boolean isOK, Boolean result, int errorCode, String errorMsg) {
                    hideLoadingView();
                    if (isOK && result) {
                        LocalUtils.showToast(SendCommentActivity.this, "评论发布成功！");
                        finish();
                    } else {
                        LocalUtils.showToast(SendCommentActivity.this, "评论发布失败！");
                    }
                }

                @Override
                public void onInternError(int errorCode, String errorMessage) {
                    hideLoadingView();
                    LocalUtils.showToast(SendCommentActivity.this, "评论发布失败！");
                }
            });
        }else{
            NetManager.getInstance(this).doPostCommComment(subjectId, msgContent, type, new OnResponseListener<Boolean>() {
                @Override
                public void onComplete(boolean isOK, Boolean result, int errorCode, String errorMsg) {
                    hideLoadingView();
                    if (isOK && result) {
                        LocalUtils.showToast(SendCommentActivity.this, "评论发布成功！");
                        finish();
                    } else {
                        LocalUtils.showToast(SendCommentActivity.this, "评论发布失败！");
                    }
                }

                @Override
                public void onInternError(int errorCode, String errorMessage) {
                    hideLoadingView();
                    LocalUtils.showToast(SendCommentActivity.this, "评论发布失败！");
                }
            });
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        init();
    }

    private void init(){
//		if(!TextUtils.isEmpty(getIntent().getStringExtra(EXTRA_DES))) {
//			setTitle(getIntent().getStringExtra(EXTRA_DES));
//		} else {
//			setTitle(getString(R.string.send_msg));
//		}
//
//		showBackView();
////		hideBackIcon();
//		setMoreText(getString(R.string.btn_send));

//		showMoreView(new OnClickListener() {
//
//			@Override
//			public void onClick(View arg0) {
//				if(mText.getText() != null && mText.getText().length() > 0){
//					Intent intent = new Intent();
//					intent.putExtra(DATA, mText.getText().toString());
//					SendCommentActivity.this.setResult(RESULT_OK, intent);
//					finish();
//				}
//			}
//		});
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) SendCommentActivity.this
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(mText,
                        InputMethodManager.RESULT_UNCHANGED_SHOWN);
            }
        }, 300);

        mText.setMaxLength(100);
        mText.setMaxPasteLength(100);
        mText.setMinLines(1);
        mText.setMaxLines(8);

        try{
            mInfo = (SnsCommentInfo)getIntent().getSerializableExtra(COMMENT_DATA);
        } catch (Exception e){
            e.printStackTrace();
        }

        mText.setHint(getString(R.string.square_send_msg_hint));
        mText.setOrientation(LinearLayout.VERTICAL);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            LogUtils.i("dispatchKeyEvent");
            finish();
        }
        return super.dispatchKeyEvent(event);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * Description
     * @param newConfig
     * @see android.app.Activity#onConfigurationChanged(android.content.res.Configuration)
     */

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        if(newConfig.keyboardHidden == Configuration.KEYBOARDHIDDEN_YES){
            finish();
        }
    }

    public void showLoadingView(String msg) {
        if (mLoadingDialog == null) {
            mLoadingDialog = DialogUtil.showLoadingDialog(mContext, msg, true);
        }

        mLoadingDialog.setDlgMessage(msg);

        if (!mLoadingDialog.isShowing() && !isFinishing()) {
            mLoadingDialog.show();
        }
    }

    public void hideLoadingView() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing() && !isFinishing()) {
            mLoadingDialog.dismiss();
        }
    }

}

